package com.liuwa.knowledge.controller;

import java.util.List;

import com.liuwa.common.utils.bean.BeanUtils;
import com.liuwa.common.exception.ServiceException;
import com.liuwa.common.utils.poi.ExportResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.liuwa.common.annotation.Log;
import com.liuwa.common.core.controller.BaseController;
import com.liuwa.common.enums.BusinessType;
import com.liuwa.knowledge.domain.Module;
import com.liuwa.knowledge.query.ModuleQuery;

import com.liuwa.knowledge.service.ModuleService;
import com.liuwa.common.utils.poi.ExcelUtil;
import com.liuwa.common.bean.Page;

/**
 * 模块Controller
 * 
 * @author liuwa
 * @date 2025-04-03 08:31:16
 */
@RestController
@Api(value = "模块")
@RequestMapping("/knowledge/modules")
public class ModuleController extends BaseController{

    @Autowired
    private ModuleService moduleService;

    /**
     * 查询模块列表
     */
    @ApiOperation("查询模块列表")
    @PreAuthorize("@ss.hasPermi('knowledge:module:list')")
    @GetMapping
    public Page<Module> list(ModuleQuery query)
    {
        Module condition = new Module();
        BeanUtils.copyBeanNotNull2Bean(query, condition);
        startPage(query);
        List<Module> list = moduleService.findList(condition);
        return getPage(list, query);
    }


    /**
     * 导出模块列表
     */
    @ApiOperation("导出模块")
    @PreAuthorize("@ss.hasPermi('knowledge:module:export')")
    @Log(title = "导出模块", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public ExportResult export(ModuleQuery query)
    {
        Module condition = new Module();
        BeanUtils.copyBeanNotNull2Bean(query, condition);
        List<Module> list = moduleService.findList(condition);
        ExcelUtil<Module> util = new ExcelUtil<Module>(Module.class);
        return util.exportExcel(list, "模块数据");
    }

    /**
     * 获取模块详细信息
     */
    @ApiOperation("获取模块详细信息")
    @PreAuthorize("@ss.hasPermi('knowledge:module:query')")
    @GetMapping(value = "/{id}")
    public Module get(@PathVariable("id") Integer id)
    {
        return moduleService.get(id);
    }

    /**
     * 新增模块
     */
    @ApiOperation("新增模块")
    @PreAuthorize("@ss.hasPermi('knowledge:module:add')")
    @Log(title = "新增模块", businessType = BusinessType.INSERT)
    @PostMapping
    public void add(@RequestBody Module module)
    {
        moduleService.insert(module);
    }

    /**
     * 修改模块
     */
    @ApiOperation("修改模块")
    @PreAuthorize("@ss.hasPermi('knowledge:module:edit')")
    @Log(title = "修改模块", businessType = BusinessType.UPDATE)
    @PutMapping
    public void edit(@RequestBody Module module)
    {
        Module oldModule = moduleService.get(module.getId());
        if(oldModule == null){
            throw new ServiceException("操作的模块不存在或已删除");
        }
        BeanUtils.copyBeanNotNull2Bean(module, oldModule);
        moduleService.update(oldModule);
    }



    /**
     * 删除模块
     */
    @ApiOperation("删除模块")
    @PreAuthorize("@ss.hasPermi('knowledge:module:remove')")
    @Log(title = "删除模块", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public void remove(@PathVariable Integer[] ids)
    {
		moduleService.batchDeleteByLogic(ids);
    }
	
	
	
    /**
     * 查询模块已删除列表
     */
    @ApiOperation("查询模块已删除列表")
    @PreAuthorize("@ss.hasPermi('knowledge:module:trash')")
    @GetMapping("/deleted")
    public Page<Module> deleted(ModuleQuery query)
    {
        Module condition = new Module();
        BeanUtils.copyBeanNotNull2Bean(query, condition);
        startPage();
        List<Module> list = moduleService.findDeletedList(condition);
        return getPage(list, query);
    }



    /**
     * 恢复模块
     */
    @ApiOperation("恢复模块")
    @PreAuthorize("@ss.hasPermi('knowledge:module:restore')")
    @Log(title = "恢复模块", businessType = BusinessType.UPDATE)
    @PutMapping("/{ids}/restore")
    public void restore(@PathVariable Integer[] ids)
    {
        moduleService.batchRestore(ids);
    }
}
